from mininet.topo import Topo
from mininet.net import Mininet
from mininet.link import TCLink
from mininet.cli import CLI
from mininet.node import OVSController
import sys

nHosts = 500
if len(sys.argv) == 2:
    nHosts = int(sys.argv[1])

class TestTopo(Topo):
    def __init__(self):
        Topo.__init__(self)
        sw = self.addSwitch('s0')
        gate = self.addHost('gate', ip='10.0.0.1')
        self.addLink(gate, sw, loss=0)
        for i in range(nHosts):
            h = self.addHost('h%d' % i)
            self.addLink(h, sw)


class MyCLI(CLI):
    def __init__(self, mn):
        CLI.__init__(self, mn)

    def do_start_server(self, line):
        self.mn.getNodeByName('gate').cmd('python server2.py &')

    def do_test(self, line):
        for i in range(nHosts):
            h = self.mn.getNodeByName('h%d' % i)
            h.cmd('python client.py >> log &')


topo = TestTopo()
net = Mininet(topo=topo, link=TCLink, controller=OVSController)
net.start()
MyCLI(net)
net.stop()
